package juego.main;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.io.File;
import java.awt.event.*;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;

import javax.swing.WindowConstants;
import javax.swing.SwingUtilities;

import juego.entidades.Entidad;
import juego.escenario.*;
import juego.graficos.*;
import juego.roles.*;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class GUI extends javax.swing.JFrame {
	private JPanel panelComponentes;
	private JPanelExtended panelLevel;
	private JPanelExtended panelFondo;
	private Image imagenLevel;
	private Image imagenFondo;
	private boolean lock = false;
	private boolean lockUp = false;
	private Actualizador levelUpdater;
	private ActualizadorUp levelUpdaterUp;
	private Thread hiloMario;
	private Jugador mario;
	private Mario labelMario;
	private Nivel level;

	/**
	* Auto-generated main method to display this JFrame
	*/
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				GUI inst = new GUI();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}
	
	public GUI() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			{
				{
					this.mario = new Jugador();
					labelMario = mario.getMario();
					labelMario.setBounds(32, 330, 55, 77);
					getContentPane().add(labelMario);
					this.hiloMario = new Thread(labelMario);
					this.hiloMario.start();
				}
				{
					java.net.URL imagenURL = getClass().getClassLoader().getResource("juego/imagenes/level.gif");
					imagenLevel = Toolkit.getDefaultToolkit().getImage(imagenURL);
					panelLevel = new JPanelExtended(imagenLevel);
					//getContentPane().add(panelLevel);
					panelLevel.setLayout(null);
					//panelLevel.setDoubleBuffered(true);
					panelLevel.setBounds(0, 0, 6775, 448); //1260 441
					panelLevel.setPreferredSize(new java.awt.Dimension(3500, 441));
					panelLevel.setBackground(new java.awt.Color(107,136,255));
				}
				{
					java.net.URL imagenURL = getClass().getClassLoader().getResource("juego/imagenes/fondo.gif");
					imagenFondo = Toolkit.getDefaultToolkit().getImage(imagenURL);
					panelFondo = new JPanelExtended(imagenFondo);
					//getContentPane().add(panelFondo);
					panelFondo.setLayout(null);
					//panelFondo.setDoubleBuffered(true);
					panelFondo.setBounds(0, 0, 6775, 448); //1260 441
					panelFondo.setPreferredSize(new java.awt.Dimension(3500, 441));
					panelFondo.setBackground(new java.awt.Color(107,136,255));
				}	
				{
					panelComponentes = new JPanel();
					panelComponentes.setLayout(null);
					//panelComponentes.setDoubleBuffered(true);
					getContentPane().add(panelComponentes);
					panelComponentes.setBackground(new java.awt.Color(107,136,255));
					panelComponentes.setSize(9600, 448);
				}
			}
						
			this.addKeyListener(new KeyAdapter() {
				public void keyReleased(KeyEvent evt) {
					thisKeyReleased(evt);
				}
				public void keyPressed(KeyEvent evt) {
					thisKeyPressed(evt);
				}
			});
			
			CreatorByFile cbf = new CreatorByFile();
			this.level = cbf.getLevel();
			loadGraphicLevel(level);
			
			pack();
			Dimension pantallaTamano = Toolkit.getDefaultToolkit().getScreenSize();
	        this.setLocation((pantallaTamano.width/2)-(this.getWidth()/2), (pantallaTamano.height/2)-(this.getHeight()/2));
			setSize((int)(pantallaTamano.width), 475);
			setResizable(false);
			setTitle("Super Mario Bros");
			
		} catch (Exception e) {
		    //add your error handling code here
			e.printStackTrace();
		}
	}
	
	private void loadGraphicLevel( Nivel level )
	{
		for( int i = 0; i < 14; i++ )
		{
			for( int j = 0; j < 300; j++ )
			{
				Entidad e = level.getEntidad(i, j);
				ElementoGrafico eg = e.getGrafico();
				eg.setLocation(j*32, i*32);
				this.panelComponentes.add(eg);
				
			}
		}
	}
	
	private void thisKeyPressed(KeyEvent evt) {
		
		if( evt.getKeyCode() == KeyEvent.VK_RIGHT)
		{
			if( !this.lock)
			{				
				labelMario.goDerecha();
				this.lock = true;
				if( this.levelUpdater == null )
				{
					this.levelUpdater = new Actualizador(this.labelMario, this.level);
					this.levelUpdater.start();
				}
			}
		} else
		{
			if( evt.getKeyCode() == KeyEvent.VK_UP)
			{
				if (!this.lockUp)
				{
					labelMario.goSalto();
					this.lock = false;
					this.levelUpdaterUp = new ActualizadorUp(this.labelMario);
					this.levelUpdaterUp.start();
					this.lockUp = true;
				}
			}
		}
	}
	
	private void thisKeyReleased(KeyEvent evt) {
			
		if( evt.getKeyCode() == KeyEvent.VK_RIGHT)
		{
			this.labelMario.goQuieto();
			this.lock = false;
			this.levelUpdater.stop();
			this.levelUpdater = null;
		} else
		{
			if( evt.getKeyCode() == KeyEvent.VK_UP)
			{
				this.lockUp = false;
			}
		}
	}
}
